//  Copyright (c) 2002-2009 "Neo Technology,"
//      Network Engine for Objects in Lund AB [http://neotechnology.com]
// 
//  This file is part of Neo4j.
//  
//  Neo4j is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Affero General Public License as
//  published by the Free Software Foundation, either version 3 of the
//  License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Affero General Public License for more details.
//  
//  You should have received a copy of the GNU Affero General Public License
//  along with this program. If not, see <http://www.gnu.org/licenses/>.
#region Using Directives

using System.Collections;
using System.Collections.Generic;
using org.neo4j.kernel.impl.util;
using Node = org.neo4j.graphdb.Node;
using TraversalPosition = org.neo4j.graphdb.TraversalPosition;
using Traverser = org.neo4j.graphdb.Traverser;

#endregion
namespace org.neo4j.kernel.impl.traversal
{
    /// <summary>  
    /// This class is used for traversers that has been sorted. A normal traverser
    /// will return a <CODE>SortedTraverser</CODE> when the <CODE>sort</CODE>
    /// method is invoked. Since a sorting operation requires one to go throu all
    /// elements that should be sorted the <CODE>SortedTraverser</CODE> just
    /// encapsulates an iterator to the sorted list of nodes. 
    /// </summary>
	internal class SortedTraverser : Traverser
    {
        #region Private Variables

        private IEnumerator<Node> nodesIterator = null;

        #endregion

        #region Class Constructors

        /// <summary> 
        /// Creates a sorted traverser from the sorted list of <CODE>nodes</CODE>
        /// </summary>
        /// <param name="nodes"> the list of sorted nodes </param>    
        internal SortedTraverser(List<Node> nodes)
        {
            this.nodesIterator = nodes.GetEnumerator();
        }

        #endregion

        #region Implementations

        public virtual object Next()
        {
            return NextNode();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual bool HasNext()
        {
            return nodesIterator.MoveNext();
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Remove()
        {
            throw new UnsupportedOperationException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual Node NextNode()
        {
            return nodesIterator.Current;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual List<Node> GetAllNodes()
        {
            // Temp storage
            List<Node> tempList = new List<Node>();

            // Traverse until the end, my beautiful friend
            //while (this.MoveNext())
            //{
            //    tempList.Add(this.NextNode());
            //}
            while (this.GetEnumerator().MoveNext())
            {
                tempList.Add(this.NextNode());
            }
            // Return nodes
            return tempList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nsi"></param>
        /// <returns></returns>
        public virtual Traverser Sort(NodeSortInfo<Node> nsi)
        {
            List<Node> tempList = new List<Node>();

            // Traverse and get all remaining nodes
            //while (this.MoveNext())
            //{
            //    tempList.Add(this.nextNode());
            //}
            while (this.GetEnumerator().MoveNext())
            {
                tempList.Add(this.NextNode());
            }
            tempList.Sort(nsi);
            return new SortedTraverser(tempList);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual TraversalPosition CurrentPosition()
        {
            // TODO Auto-generated method stub
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<Node> Iterator()
        {
            // TODO Auto-generated method stub
            return null;
        }

        #region IEnumerable<Node> Members

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<Node> GetEnumerator()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        #endregion
    }
}